package com.anjie.powerproject.controller;

import com.anjie.powerproject.entity.EnergyType;
import com.anjie.powerproject.service.EnergyTypeService;
//import com.anjie.powerproject.util.FtpUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static com.anjie.powerproject.controller.DeviceController.*;

@Controller
@RequestMapping("energyType")
public class EnergyTypeController {

    @Autowired
    private EnergyTypeService energyTypeService;

    /**
     * 添加能源属性类型，包括图片和基本信息
     * @param energyType
     * @param icon
     * @return
     * @throws IOException
     */
    @PostMapping("/addEnergyType")
    public String addEnergyType(HttpServletRequest request, EnergyType energyType, @RequestParam("icon")MultipartFile icon) throws IOException {
        String base = request.getServletContext().getRealPath("WEB-INF/classes/static/images/");

        if (icon!=null){
            if (!"".equals(icon.getOriginalFilename())) {
                String fileName = UUID.randomUUID().toString() + icon.getOriginalFilename().substring(icon.getOriginalFilename().lastIndexOf("."));
//                icon.transferTo(new File(base + fileName));
                File imgFile = new File(base + fileName);
                FileUtils.copyInputStreamToFile(icon.getInputStream(), imgFile);
                energyType.setIconPath("/multipower/images/" + fileName);

            }
        }
        energyTypeService.addOneEnergyType(energyType);
        return "redirect:/energyType/viewEnergyType";
    }

    /**
     * 查看所有的能源属性
     * @param model
     * @return
     */
    @GetMapping("/viewEnergyType")
    public String viewEnergyType(Model model){
        List<EnergyType> allEnergyType = energyTypeService.getAllEnergyType();
        model.addAttribute("energyTypeList",allEnergyType);
        return "property";
    }

    /**
     * 跳转到能源属性修改界面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/get/{id}")
    public String getEnergyTypeInfo(@PathVariable("id")Integer id,Model model){
        EnergyType energyTypeById = energyTypeService.findEnergyTypeById(id);
        model.addAttribute("energyType",energyTypeById);
        return "propertyEdit";
    }

    /**
     * 更新能源属性信息
     * @param energyType
     * @param icon
     * @return
     * @throws IOException
     */
    @PostMapping("/update")
    public String updateEnergyTypeInfo(HttpServletRequest request, EnergyType energyType,@RequestParam("icon")MultipartFile icon) throws IOException {
        String base = request.getServletContext().getRealPath("WEB-INF/classes/static/images/");

        EnergyType energyTypeById = energyTypeService.findEnergyTypeById(energyType.getTypeId());
        energyTypeById.setTypeName(energyType.getTypeName());
        if (icon!=null){
            String ftpPath = "/"+UPLOAD_IMG;
            if (!"".equals(icon.getOriginalFilename())) {
                String fileName = UUID.randomUUID().toString() + icon.getOriginalFilename().substring(icon.getOriginalFilename().lastIndexOf("."));
                icon.transferTo(new File(base + fileName));
                energyTypeById.setIconPath("/multipower/images/" + fileName);
            }
        }
        energyTypeService.saveAndFlush(energyTypeById);
        return "redirect:/energyType/viewEnergyType";
    }

    /**
     * 获取所有的能源属性信息json格式
     * @return
     */
    @GetMapping("/getAllEnergyType")
    @ResponseBody
    public List getAllEnergyType(){
        List<EnergyType> allEnergyType = energyTypeService.getAllEnergyType();
        return allEnergyType;
    }

    /**
     * 校验设备能源属性name是否重复
     * @param energyTypeName
     * @return
     */
    @GetMapping("/check")
    @ResponseBody
    private Map checkDuplicateTypeName(@RequestParam("energyTypeName")String energyTypeName){
        Map map = new HashMap();
        StringBuilder sb = new StringBuilder();
        boolean b = energyTypeService.checkEnergyTypeName(energyTypeName);
        if (b) sb.append("yes");
        else sb.append("no");
        map.put("check",sb.toString());
        return map;
    }

    /**
     * 能源属性的删除，删除该属性是危险操作
     */
    @GetMapping("/deleteEnergyType/{typeId}")
    @ResponseBody
    public Map deleteEnergyType(@PathVariable("typeId")Integer typeId){
        Map map = new HashMap();
        boolean b = energyTypeService.deleteEnergyType(typeId);
        if (b) map.put("message","success");
        else map.put("message","fail");
        return map;
    }
}
